package com.netx.cubigraf.apps.utilities;
import java.io.IOException;
import java.io.PrintWriter;
import com.netx.generics.io.BufferedWriter;
import com.netx.generics.io.Directory;
import com.netx.generics.io.File;
import com.netx.generics.io.FileSystem;
import com.netx.generics.io.Location;


public class PRJ10_FileLister {

	public static void run() throws Throwable {
		FileSystem root = new FileSystem(new Location("C:/Shared/Repository/Tests/cubigraf2/Server"));
		FileSystem csv = new FileSystem(new Location("C:/Shared/Repository/Tests/cubigraf2"));
		PRJ10_FileLister lister = new PRJ10_FileLister(root, csv);
		lister.listFolders();
		lister.listZips();
	}

	private final Directory _root;
	private final Directory _csv;
	private final Directory _ficheiros;
	private final Directory _zips;
	private final String[] _years = {"2004", "2005"};
	private final String[] _months = {"01","02","03","04","05","06","07","08","09","10","11","12"};
	
	public PRJ10_FileLister(Directory root, Directory csv) throws IOException {
		_root = root;
		_csv = csv;
		_ficheiros = _root.getDirectory("Ficheiros");
		_zips = _root.getDirectory("Zips");
	}

	public void listFolders() throws IOException {
		_list("folder-list.csv", _ficheiros);
	}

	public void listZips() throws IOException {
		_list("zips-list.csv", _zips);
	}
	
	private void _list(String fileName, Directory src) throws IOException {
		File file = _csv.getFile(fileName);
		if(file == null) {
			file = _csv.createFile(fileName);
		}
		PrintWriter out = new PrintWriter(new BufferedWriter(file.getOutputStream(false)));
		out.println("Ficheiro");
		for(int i=0; i<_years.length; i++) {
			for(int j=0; j<_months.length; j++) {
				Directory tmp = src.getDirectory(_years[i]+"/"+_months[j]);
				if(tmp != null) {
					String[] all = tmp.listAll();
					for(int k=0; k<all.length; k++) {
						out.println(all[k]);
					}
				}
			}
		}
		out.close();
	}
}
